/*!\file  interrupt.c  IRQ handlers.
 *
 *      This file is part of the micro-rtos package.
 *
 *  Copyright (c) 2008-2009 by 
		Gabriel Zabusek <gabriel.zabusek@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#include <boot/lpc2103.h>
#include <kernel/interrupt.h>
#include <include/std_io.h>
#include <include/types.h>
#include <kernel/list.h>
#include <kernel/task.h>
#include <kernel/sched.h>

void irq_h(void)
{

if( T0IR & 0x00000002 ){

	IRQ_TIMER_TICK++;

	if( IRQ_TIMER_TICK % 2 == 0 )
		IOCLR = (1<<17);
	else
		IOSET = (1<<17);

	SaveContext(pCurTaskStackTop);
	pCurRunningTask->pStackTop = pCurTaskStackTop;

	list_move_tail ((&ready_list.list)->next, &(ready_list.list));
	task_list_t * next_task = list_entry ((&ready_list.list)->next, task_list_t, list);

	pCurRunningTask = next_task->p_task;
	pCurTaskStackTop = pCurRunningTask->pStackTop;

	T0IR = 0x00000002;
	
	RestoreContext(pCurTaskStackTop);

	/*if( !bSchedulingStart )
		goto IRQ_END;

	if( bFirstRestore == TRUE ){
		SaveContext(pCurTaskStackTop);
		pCurRunningTask->pStackTop = pCurTaskStackTop;
	}

	list_move_tail ((&ready_list.list)->next, &(ready_list.list));
	volatile task_list_t * next_task = list_entry ((&ready_list.list)->next, task_list_t, list);

	pCurRunningTask = next_task->p_task;
	pCurTaskStackTop = pCurRunningTask->pStackTop;

	T0IR = 0x00000002;

	bFirstRestore = TRUE;
	
	RestoreContext(pCurTaskStackTop);*/

}

if( T0IR & 0x00000001 ){
	T0IR = 0x00000001; //clear MR0 int
}

}

void swi_h(void)
{
	printk(".");
}
